package com.huaao.micropolice.root.commons;

import java.util.List;
import java.util.Set;

import com.huaao.micropolice.core.model.content.Subject;
import com.huaao.micropolice.core.model.system.Dept;
import com.huaao.micropolice.core.model.system.Menu;

public class TreeUtils {
	
	public static void buildMenuTreeJson(StringBuffer buff, List<Menu> menus) {
		buff.append("[");
		for(int i = 0 ; i < menus.size(); i++){
			Menu m = menus.get(i);
			buff.append("{");
			buff.append("id:'" + m.getId() + "',");
			if(m.getParent() == null){
				buff.append("pId:'0',open:true,");
			}else{
				buff.append("pId:'" + m.getParent().getId() + "',");
			}
			buff.append("name:'" + m.getName()+"',");
			buff.append("menuUrl:'" + m.getUrl() + "',");
			buff.append("permission:'" + m.getPermission() + "',");
			buff.append("menuSort:'" + m.getSort() + "',");
			buff.append("menuIcon:'" + m.getIcon() + "'");
			buff.append("}");
			if(i < menus.size() -1){
				buff.append(",");
			}
		}
		buff.append("]");
	}

	public static void buildMenuTreeJson(StringBuffer buff, List<Menu> menus,
			Set<Menu> menus2) {
		buff.append("[");
		for(int i = 0 ; i < menus.size(); i++){
			Menu m = menus.get(i);
			buff.append("{");
			buff.append("id:'" + m.getId() + "',");
			if(m.getParent() == null){
				buff.append("pId:'0',open:true,");
			}else{
				buff.append("pId:'" + m.getParent().getId() + "',");
			}
			buff.append("name:'" + m.getName()+"',");
			buff.append("menuUrl:'" + m.getUrl() + "',");
			buff.append("permission:'" + m.getPermission() + "',");
			buff.append("menuSort:" + m.getSort());
			if(menus2.contains(m)){
				buff.append(",checked:true");
			}
			buff.append("}");
			if(i < menus.size() -1){
				buff.append(",");
			}
		}
		buff.append("]");
	}

	public static void buildDeptTreeJson(StringBuffer buff, List<Dept> depts) {
		buff.append("[");
		for(int i = 0 ; i < depts.size(); i++){
			Dept dept = depts.get(i);
			buff.append("{");
			buff.append("id:'" + dept.getId() + "',");
			if(dept.getParent() == null){
				buff.append("pId:'-1',open:true,");
			}else{
				buff.append("pId:'" + dept.getParent().getId() + "',");
			}
			buff.append("name:'" + dept.getName()+"',");
			buff.append("}");
			if(i < depts.size() -1){
				buff.append(",");
			}
		}
		buff.append("]");
	}
	
	public static void buildSubjectTreeJson(StringBuffer buff, List<Subject> subjects){
		buff.append("[");
		for(int i = 0 ; i < subjects.size(); i++){
			Subject sub = subjects.get(i);
			buff.append("{");
			buff.append("id:'" + sub.getId() + "',");
			if(sub.getParent() == null){
				buff.append("pId:'-1',open:true,");
			}else{
				buff.append("pId:'" + sub.getParent().getId() + "',");
			}
			buff.append("name:'" + sub.getName()+"',");
			buff.append("}");
			if(i < subjects.size() -1){
				buff.append(",");
			}
		}
		buff.append("]");
	}
}
